/**
 * @ClassName: DicPriceService
 * @author linsen
 * @date 2016-11-23
 */
package com.fzrskj.cloud.free.dic.service;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.enums.PriceTypeEnum;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CSessionTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.dic.bean.DicPrice;
import com.fzrskj.cloud.free.dic.utils.DicPriceDiyUtils;
import com.fzrskj.cloud.free.dic.utils.DicPriceUtils;
import com.fzrskj.cloud.free.user.bean.UserDiyBean;
import com.fzrskj.cloud.free.user.utils.UserDiyUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DicPriceService extends CObject {
	/**
	 * 查询价格
	 * @param req
	 * @param resp
	 * @param p={}
	 * @param sid
	 * @return
	 */
	public String queryDicPrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String hostType= CJsonTool.getStrValue(p, "hostType", "");		//注册商
		String chargeItem=CJsonTool.getStrValue(p, "chargeItem", "");  //计费项
		String regionId=CJsonTool.getStrValue(p, "regionId", "");      //地域
		String modelId=CJsonTool.getStrValue(p, "modelId", "");        //机型(当chargeItem为spec用)
		
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("hostType", hostType);
		cdt.put("chargeItem", chargeItem);
		cdt.put("regionId", regionId);
		
		if(chargeItem.equals("spec")){cdt.put("modelId", modelId);}
		
		DicPriceUtils dpUtils=new DicPriceUtils();
		List<DicPrice> list=dpUtils.queryDicPriceList(cdt);
		String code=dpUtils.getErrCode();
		if (list==null) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, CJsonTool.listBeanToJson(list));
	}
	
	/**
	 * 修改价格
	 * @param req
	 * @param resp
	 * @param p={}
	 * @param sid
	 * @return
	 */
	public String uptDicPrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		//注册商
		String hostType=CJsonTool.getStrValue(p, "hostType", "");
		//计费项
		String chargeItem=CJsonTool.getStrValue(p, "chargeItem", "");
		//地域
		String regionId=CJsonTool.getStrValue(p, "regionId", "");
		//机型(当chargeItem为spec用)
		String modelId=CJsonTool.getStrValue(p, "modelId", "");
		//参数项
		String dataItem=CJsonTool.getStrValue(p, "dataItem", "");
		
		if (CStrTool.isBlankStr(hostType) ||
			CStrTool.isBlankStr(chargeItem) ||
			CStrTool.isBlankStr(regionId) ||
			CStrTool.isBlankStr(dataItem) ){
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("hostType", hostType);
		cdt.put("chargeItem", chargeItem);
		cdt.put("regionId", regionId);
		
		//先删除价格
		if(chargeItem.equals("spec")){cdt.put("modelId", modelId);}
		DicPriceUtils dpUtils=new DicPriceUtils();
		dpUtils.delDicPrice(cdt);
		String code = dpUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		
		//添加价格
		boolean result=dpUtils.addDicPrice(dataItem);
		code = dpUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultTips(result, code);
	}


	/**
	 * 获取自定义商品价格.
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String getDiyPrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
		int uid = CSessionTool.getUidCloud(sid);

		String productCode=CJsonTool.getStrValue(p, "productCode", "");
		Integer applyTime=CJsonTool.getIntValue(p, "applyTime", 0);
		String priceType=CJsonTool.getStrValue(p, "priceType", "");
		int num=CJsonTool.getIntValue(p, "num", 1);

		if (CStrTool.isBlankStr(productCode) ||
				applyTime < 1 || CStrTool.isBlankStr(priceType) || num < 1){
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("productCode", productCode);
		cdt.put("applyTime", applyTime);
		cdt.put("priceType", priceType);
		cdt.put("num", num);
		cdt.put("chargeId", 1);
		if(uid>0) {
			cdt.put("userId", uid);
		}

		//其他参数
		cdt.putAll(JSONObject.parseObject(p, Map.class));

		DicPriceDiyUtils dicPriceDiyUtils = new DicPriceDiyUtils();
		String result = dicPriceDiyUtils.getPrice(cdt);
		return result;
	}

	/**
	 * 获取续费价格.
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String getDiyRenewPrice(HttpServletRequest req, HttpServletResponse resp, String p, String sid) {
		int uid = CSessionTool.getUidCloud(sid);

		String productCode = CJsonTool.getStrValue(p, "productCode", "");
		Integer applyTime = CJsonTool.getIntValue(p, "applyTime", 0);
		String priceType = CJsonTool.getStrValue(p, "priceType", "");
		int num = CJsonTool.getIntValue(p, "num", 1);
		Integer id = CJsonTool.getIntValue(p, "id", 0);

		if (CStrTool.isBlankStr(productCode) ||
				applyTime < 1 || CStrTool.isBlankStr(priceType) || num < 1 || id<1){
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("productCode", productCode);
		cdt.put("applyTime", applyTime);
		cdt.put("priceType", priceType);
		cdt.put("num", num);
		cdt.put("id", id);
		cdt.put("chargeId", 2);
		if(uid>0) {
			cdt.put("userId", uid);
		}

		//获取开通参数
		UserDiyUtils diyUtils = new UserDiyUtils();
		UserDiyBean bean = diyUtils.queryUserDiy(id);
		if (null == bean) {
			return XmlCodeParser.formatResultTips(false, "DIY BUSINESS NOT FOUND", "自定义业务不存在");
		}
		JSONObject openInfo = JSONObject.parseObject(bean.getOpenInfo());
		JSONObject orderParams = openInfo.getJSONObject("diy_params");
		if (null == orderParams) {
			orderParams = new JSONObject();
		}
		orderParams.put("number", num);
		orderParams.put("period", applyTime);
		orderParams.put("period_unit", PriceTypeEnum.MONTH.equals(priceType) ? "month" : "year");
		orderParams.put("changId", 2);
		cdt.putAll(orderParams);

		DicPriceDiyUtils dicPriceDiyUtils = new DicPriceDiyUtils();
		String result = dicPriceDiyUtils.getPrice(cdt);
		return result;
	}

	/**
	 * 获取升级价格（自定义商品）.
	 * @return
	 */
	public String getDiyUpdatePrice(HttpServletRequest request, HttpServletResponse response, String p, String sid) {
		int uid = CSessionTool.getUidCloud(sid);

		Integer id= CJsonTool.getIntValue(p, "id", 0);

		if (id<1){
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		DicPriceDiyUtils dicPriceDiyUtils = new DicPriceDiyUtils();
		String result = dicPriceDiyUtils.getUpgradePrice(uid, id, JSONObject.parseObject(p, Map.class));
		return result;
	}

}
